//
//  ReadBookVOrH_VC+Fav.m
//  KidReading
//
//  Created by telen on 16/3/8.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ReadBookVOrH_VC+Fav.h"
#import "ConstDefine.h"

@interface ReadBookVOrH_VC()
@property(nonatomic,strong)StatusButton_2_View* favBtn;
@property(nonatomic,strong)UIButton* favBtn_2;
@property(nonatomic,assign)BOOL isF_open;
@end

@implementation ReadBookVOrH_VC (Fav)

+(void)load{
    [self swizzleSelector:@selector(tapBack) withSelector:@selector(tapBack_favor)];
}

ASSOCIATED(favBtn, setFavBtn, StatusButton_2_View*, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
ASSOCIATED(favBtn_2, setFavBtn_2, UIButton*, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
ASSOCIATED_BOOL(isF_open, setIsF_open);
//
-(void)tapBack_favor{
    [self tapBack_favor];
    if (self.isF_open != self.isFavorited) {
        [[Merge_MineAndFavor_Manager defaultManager] synchronousBooksDataWithNetWithMergeType:MergeType_Favor];
    }
}

- (void)addFavBtn
{
    CGFloat ratio = ratio_w;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        ratio /= 1.25;
    }
    CGRect frame = CGRectMake(30/ratio,0, 55/ratio, 65/ratio);
    self.favBtn = [[StatusButton_2_View alloc] initWithFrame:frame imageOn:@"collect_btn_down.png" imageOff:@"collect_btn_normal.png"];
    [self.favBtn setButtonStatus:self.isFavorited];
    [self.view addSubview:self.favBtn];
    self.favBtn.alpha = 0;
    self.isF_open = self.isFavorited;
    __weak ReadBookVOrH_VC* wself = self;
    self.favBtn.btnClicked = ^(StatusButtonView* sBtn){
        [wself clickedFavBtn];
    };
}

- (void)clickedFavBtn
{
    StatusButton_2_View* favBtn = self.favBtn;
    UIButton* btn = self.favBtn_2;
    
    if (favBtn.onOff == self.isFavorited) {
        [self msgTo_menthed:@"statistical_ToolsClicked:" refs:@[@"功能页_收藏"] needReturnValue:NO];
    }else{
        [self msgTo_menthed:@"statistical_ToolsClicked:" refs:@[@"收藏"] needReturnValue:NO];
    }
    
    [favBtn setDisAble:YES];
    btn.enabled = NO;
    [favBtn setButtonStatus:self.isFavorited];
    btn.selected = self.isFavorited;
    if (self.isFavorited) {
        [[Merge_MineAndFavor_Manager defaultManager] updateTempTableWithBookModel:self.tb_book WithTempType:TempType_MINUS MergeType:MergeType_Favor Completed:^{
            NSString* str = ReadLocalizedString(@"favcanceled");
            self.isFavorited = NO;
            if ([DataProcessing iOS7_low]) {
                [[WaitingShow shareSingleShow] showString:str inSuperView:readView andAutoHide:YES];
            }else{
                [[WaitingShow shareSingleShow] showString:str andAutoHide:YES];
            }
            [favBtn setDisAble:NO];
            btn.enabled = YES;
            [favBtn setButtonStatus:self.isFavorited];
            btn.selected = self.isFavorited;
        }];
    }else{
        [[Merge_MineAndFavor_Manager defaultManager] updateTempTableWithBookModel:self.tb_book WithTempType:TempType_ADD MergeType:MergeType_Favor Completed:^{
            NSString* str = ReadLocalizedString(@"favsuccess");
            self.isFavorited = YES;
            if ([DataProcessing iOS7_low]) {
                [[WaitingShow shareSingleShow] showString:str inSuperView:readView andAutoHide:YES];
            }else{
                [[WaitingShow shareSingleShow] showString:str andAutoHide:YES];
            }
            [favBtn setDisAble:NO];
            btn.enabled = YES;
            [favBtn setButtonStatus:self.isFavorited];
            btn.selected = self.isFavorited;
        }];
    }
}

- (void)gotToCover
{
    [DataProcessing do_Main_quick:^{
        [self gotToPageIndex:0];
    }];
}

- (void)reflashFavCover
{
    if (readView.currentPageView.pageIndex == 0){
        [UIView animateWithDuration:0.2 animations:^{
            self.favBtn.alpha = 1;
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.favBtn.alpha = 0;
        }];
    }
}

@end
